package com.woyou.ui.activity;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

import retrofit.RetrofitError;
import android.content.Intent;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.citaq.ideliver.R;
import com.woyou.bean.CodeResult;
import com.woyou.bean.Coupon;
import com.woyou.bean.DeliverTime;
import com.woyou.bean.Gift;
import com.woyou.bean.IChoose;
import com.woyou.bean.Info;
import com.woyou.bean.MyAddress;
import com.woyou.bean.ShopDetail;
import com.woyou.bean.ShopItem;
import com.woyou.bean.ShoppingCarService;
import com.woyou.bean.UserInfo;
import com.woyou.bean.rpc.PwdCheckReq;
import com.woyou.bean.rpc.ShoppingTimeReq;
import com.woyou.controller.ConfirmOrderActController;
import com.woyou.service.NetWorkCenter;
import com.woyou.ui.fragment.SelectTimeLayout;
import com.woyou.ui.fragment.SelectTimeLayout.SelectTimeListener;
import com.woyou.utils.Dialog;
import com.woyou.utils.LogUtil;
import com.woyou.utils.ParseUtils;
import com.woyou.utils.eventbus.EventRestConfirm;
import com.woyou.utils.eventbus.SendOrderData;

import de.greenrobot.event.EventBus;

/**
 * 确认订单
 * 
 * @author
 *
 */
@EActivity(R.layout.fm_confirmorder)
public class ConfirmOrderActivity extends BaseActivity {

	// 选择时间
	@ViewById
	SelectTimeLayout fm_confirmorder_selecttime_view;
	// 送出时间
	@ViewById(R.id.deliver_time)
	TextView deliverTimeTv;
	@ViewById(R.id.head_title)
	TextView headTitle;
	// 外送时间列表
	List<DeliverTime> deliverTimes;

	@ViewById
	ScrollView fm_confirmorder_scroll;
	@ViewById
	RelativeLayout fm_confirmorder_selecttime;
	// 添加地址
	@ViewById
	RelativeLayout fm_confirmorder_putaddr, fm_confirmorder_chooseaddr;
	// 地址文本
	@ViewById
	TextView fm_confirmorder_addr;
	// 姓名文本
	@ViewById
	TextView fm_confirmorder_name;
	// 性别文本
	@ViewById
	TextView fm_confirmorder_sex;
	// 手机号文本
	@ViewById
	TextView fm_confirmorder_phone;
	// 管理地址
	@ViewById
	RelativeLayout fm_confirmorder_myAddr;
	// 添加备注
	@ViewById
	RelativeLayout fm_confirmorder_remarks;
	// 备注内容
	@ViewById(R.id.fm_confirmorder_remarks_tx)
	public TextView remarkTx;
	// 优惠卷
	@ViewById
	RelativeLayout fm_confirmorder_coupons;
	@ViewById
	TextView fm_confirmorder_coupons_tx;
	@ViewById
	RelativeLayout fm_confirmorder_coupons_view;
	@ViewById
	ImageView coupons_arrow_close;
	@ViewById
	TextView fm_confirmorder_coupons_value;

	// 是否需要发票
	@ViewById
	CheckBox fm_confirmorder_bill_cb;
	@ViewById
	RelativeLayout fm_confirmorder_bill_layout;

	// 价格明细
	// @ViewById
	// ListView fm_confirmorder_lv;
	@ViewById
	LinearLayout fm_confirmorder_layout;
	@ViewById
	TextView fm_confirmorder_count;
	@ViewById
	TextView fm_confirmorder_sum;

	// 发送订单
	@ViewById
	RelativeLayout fm_confirmorder_submit;
	// 返回按钮
	@ViewById
	RelativeLayout back;

	// //订单状态
	// @ViewById
	// OrderStatusLayout fm_confirmorder_orderstatus;

	// private ConfirmOrderAdapter confirmOrderAdapter;
	private List<Object> confirmOrderData = new ArrayList<Object>();

	// 获取外卖时间段
	private ShoppingTimeReq shoppingTimeReq;
	// 备注
	public String remarkContent = null;
	// 选择或者新建的地址
	public MyAddress addr = null;
	// 外卖时间
	public DeliverTime deliverTime;
	// 时间戳
	public long currentTime = System.currentTimeMillis();
	public ShopItem shopItem; // 店铺item
	public ShopDetail shopDetail; // 店铺详情

	// 优惠券
	List<Coupon> coupon;

	String today;
	private UserInfo userInfo;

	// ----------支付------------------
	public ImageView onlinePayImage;
	public ImageView deliverPayImage;
	public RelativeLayout onlineLayout;
	public RelativeLayout deliverLayout;

	private ConfirmOrderActController mActController;

	/**重置确认订单页面*/
	public void onEvent(EventRestConfirm event) {
		fm_confirmorder_addr.setText("");
		fm_confirmorder_name.setText("");
		fm_confirmorder_sex.setText("");
		fm_confirmorder_phone.setText("");
		deliverTimeTv.setText("");
		addr = null;
		deliverTimes = null;
		deliverTime = null;
		remarkContent = "";
	}

	// 初始化页面
	@AfterViews
	public void init() {
		// queryUserInfo();
		obtainInfo();
		initLayout();
	}

	/** 初始化view */
	private void initLayout() {
		mActController = new ConfirmOrderActController(mContext, this);
		// ----------支付------------------
		onlinePayImage = (ImageView) findViewById(R.id.pay_online_image);
		deliverPayImage = (ImageView) findViewById(R.id.pay_deliver_image);
		onlineLayout = (RelativeLayout) findViewById(R.id.pay_online);
		onlineLayout.setOnClickListener(this);
		deliverLayout = (RelativeLayout) findViewById(R.id.pay_deliver);
		deliverLayout.setOnClickListener(this);
		onlineLayout.setVisibility(View.GONE);
		if (shopDetail.getSupportList() != null) {
			if (shopDetail.getIsOnlinePay() == 1) {
				onlineLayout.setVisibility(View.VISIBLE);
			} else {
				deliverPayImage.setImageResource(R.raw.rediobox1);
			}
		} else {
			deliverPayImage.setImageResource(R.raw.rediobox1);
		}
	}

	/**
	 * 刷新订单数据
	 */
	boolean isDelete = false;

	public void refreshData() {
		mShoppingCarModel.calculate();
		float sumMoney = mShoppingCarModel.getTotalCost();
		fm_confirmorder_count
				.setText(Html.fromHtml("<small>共</small> " + mShoppingCarModel.getTotal() + " <small>份</small>"));
		fm_confirmorder_sum.setText(Html.fromHtml("<small>¥</small>" + ParseUtils.formatPrice(sumMoney)));
		if (!TextUtils.isEmpty(remarkContent)) {
			remarkTx.setText(remarkContent);
		} else {
			remarkTx.setText("");
		}
		if (addr != null && mUserModel.queryAddrList().size() > 0) {
			for (int i = 0; i < mUserModel.queryAddrList().size(); i++) {
				if (addr.getAddrId().equals(mUserModel.queryAddrList().get(i).getAddrId())) {
					isDelete = false;
					break;
				} else {
					isDelete = true;
				}
			}
		} else {
			isDelete = true;
		}

		// 设置地址
		if (addr != null && !isDelete) {
			fm_confirmorder_name.setText(addr.getContact());
			String sex = "";
			if (addr.getSex() == 2) {
				sex = "先生";
			} else if (addr.getSex() == 1) {
				sex = "女士";
			}
			fm_confirmorder_sex.setText(sex + "");
			fm_confirmorder_phone.setText(addr.getPhone());
			fm_confirmorder_addr.setText("" + addr.getAddrName());
			fm_confirmorder_chooseaddr.setVisibility(View.GONE);
			fm_confirmorder_myAddr.setVisibility(View.VISIBLE);
			fm_confirmorder_putaddr.setVisibility(View.GONE);
		} else if (mUserModel.queryAddrList() != null && mUserModel.queryAddrList().size() > 0) {
			// showUI();
			fm_confirmorder_chooseaddr.setVisibility(View.VISIBLE);
			fm_confirmorder_myAddr.setVisibility(View.GONE);
			fm_confirmorder_putaddr.setVisibility(View.GONE);
		} else {
			fm_confirmorder_myAddr.setVisibility(View.GONE);
			fm_confirmorder_putaddr.setVisibility(View.VISIBLE);
			fm_confirmorder_chooseaddr.setVisibility(View.GONE);
		}
		// 优惠券
		setCouponsView();
	}

	/**
	 * 设置优惠卷
	 */
	public void setCouponsView() {
		coupon = mShoppingCarModel.getCouponList();
		if (coupon != null && coupon.size() > 0) {
			float couponValue = 0;
			for (int i = 0; i < coupon.size(); i++) {
				String value = coupon.get(i).getcValue();
				try {
					float cv = Float.parseFloat(value);
					couponValue += cv;
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			String couponStr = "";
			couponStr = "-￥" + ParseUtils.String2Money(couponValue);
			fm_confirmorder_coupons_value.setText(couponStr);
			fm_confirmorder_coupons_view.setVisibility(View.VISIBLE);
			fm_confirmorder_coupons_tx.setVisibility(View.GONE);
		} else {
			fm_confirmorder_coupons_view.setVisibility(View.GONE);
			fm_confirmorder_coupons_tx.setVisibility(View.VISIBLE);
		}

	}

	/**
	 * 初始化数据
	 */
	public void initData() {
		mShoppingCarModel.calculate();
		float sumMoney = mShoppingCarModel.getTotalCost();
		fm_confirmorder_count
				.setText(Html.fromHtml("<small>共</small> " + mShoppingCarModel.getTotal() + " <small>份</small>"));
		fm_confirmorder_sum.setText(Html.fromHtml("<small>¥</small>" + ParseUtils.formatPrice(sumMoney)));

		// 判断地址是否为空
		if (mUserModel.queryAddrList() != null && mUserModel.queryAddrList().size() > 0) {
			fm_confirmorder_chooseaddr.setVisibility(View.VISIBLE);
			fm_confirmorder_myAddr.setVisibility(View.GONE);
			fm_confirmorder_putaddr.setVisibility(View.GONE);
		} else {
			fm_confirmorder_myAddr.setVisibility(View.GONE);
			fm_confirmorder_putaddr.setVisibility(View.VISIBLE);
			fm_confirmorder_chooseaddr.setVisibility(View.GONE);
		}
		// 设置优惠卷
		setCouponsView();
		initShoppingData();
	}

	public void showUI() {
		if (mUserModel.queryAddrList().size() > 0) {
			addr = mUserModel.queryAddrList().get(0);
			fm_confirmorder_name.setText(addr.getContact());
			String sex = "";
			if (addr.getSex() == 2) {
				sex = "先生";
			} else if (addr.getSex() == 1) {
				sex = "女士";
			}
			fm_confirmorder_sex.setText(sex + "");
			fm_confirmorder_phone.setText(addr.getPhone());
			fm_confirmorder_addr.setText("" + addr.getAddrName());
			fm_confirmorder_chooseaddr.setVisibility(View.GONE);
			fm_confirmorder_myAddr.setVisibility(View.VISIBLE);
			fm_confirmorder_putaddr.setVisibility(View.GONE);
		}
	}

	/**获取外卖时间段*/
	public void initDeliverTime() {
		// 获取外卖时间段
		shoppingTimeReq = new ShoppingTimeReq();
		shoppingTimeReq.setsId(shopDetail.getsId());
		executeTask(new Runnable() {
			@Override
			public void run() {
				try {
					deliverTimes = mOrderModel.getShoppingTimeList(shoppingTimeReq).getList();
					if (deliverTimes != null) {
						// 重组数据
						resetData(deliverTimes);
						runOnUI(new Runnable() {
							public void run() {
								DeliverTime t = deliverTimes.get(0);
								if(t!=null && t.getContent().equals("尽快送达")){
									deliverTime = deliverTimes.get(0);
									deliverTimeTv.setText(deliverTime.getContent());
								} else {
									deliverTimeTv.setText("");
									deliverTimeTv.setHint("希望何时送达");
								}
							}
						});

					}
				} catch (RetrofitError e) {
					e.printStackTrace();
				}

			}
		});
	}

	/**
	 * 初始化购物车里面的商品
	 */
	public void initShoppingData() {
		confirmOrderData.clear();
		List<IChoose> list = mShoppingCarModel.getChosenGoodsList();
		confirmOrderData.addAll(list);
		// 优惠活动
		// 活动页面
		List<Gift> giftList = mActivitiesModel.getGiftList();
		for (int i = 0; i < giftList.size(); i++) {
			Gift gift = giftList.get(i);
			if (gift.getQty() > 0) {
				confirmOrderData.add(gift);
			}
		}
		// 外送费和餐盒费
		if (shopDetail != null) {
			// 如果外送费大于0，显示外送费
			if (shopDetail.getSendFee() > 0) {
				ShoppingCarService service = new ShoppingCarService();
				service.setName("外送费");
				service.setPrice("" + ParseUtils.String2Money(shopDetail.getSendFee()));
				confirmOrderData.add(service);
			}
			// 餐盒费大于0，显示餐盒费
			if (shopDetail.getMealsBoxFee() > 0) {
				ShoppingCarService service1 = new ShoppingCarService();
				service1.setName("餐盒费");
				service1.setPrice("" + ParseUtils.String2Money(mShoppingCarModel.getMealsBoxFee()));
				confirmOrderData.add(service1);
			}
		}
		// 判断是否有优惠券
		coupon = mShoppingCarModel.getCouponList();
		if (coupon != null && coupon.size() > 0) {
			for (int i = 0; i < coupon.size(); i++) {
				confirmOrderData.add(coupon.get(i));
			}
		}
		// if (confirmOrderAdapter == null) {
		// confirmOrderAdapter = new
		// ConfirmOrderAdapter(getActivity(),confirmOrderData);
		// fm_confirmorder_lv.setAdapter(confirmOrderAdapter);
		// }
		// confirmOrderAdapter.notifyDataSetChanged();
		createNewView(confirmOrderData);
	}

	/**
	 * 生成订单详情列表
	 */
	public void createNewView(List<Object> confirmOrderData) {
		fm_confirmorder_layout.removeAllViews();
		int size = confirmOrderData.size();
		for (int i = 0; i < confirmOrderData.size(); i++) {
			View convertView = View.inflate(mActivity, R.layout.item_pricelist_lv, null);
			TextView name = (TextView) convertView.findViewById(R.id.item_pricelist_lv_name);
			TextView num = (TextView) convertView.findViewById(R.id.item_pricelist_lv_num);
			TextView price = (TextView) convertView.findViewById(R.id.item_pricelist_lv_price);
			LinearLayout adjust = (LinearLayout) convertView.findViewById(R.id.item_goods_adjust);
			TextView prop = (TextView) convertView.findViewById(R.id.item_goods_prop);
			adjust.setVisibility(View.GONE);

			Object obj = confirmOrderData.get(i);
			if (obj instanceof IChoose) {
				IChoose cg = (IChoose) obj;
				if (cg.getChosenOptList().size() > 0 || cg.getChosenSGList().size() > 0) {
					mShoppingCarModel.restName(cg);
					adjust.setVisibility(View.VISIBLE);
					prop.setText(Html.fromHtml(cg.getNames()));
				} else {
					adjust.setVisibility(View.GONE);
				}
				name.setText(Html.fromHtml(cg.getgName()));
				num.setText("×" + cg.getChosenNum());
				price.setText(
						Html.fromHtml("<small>￥</small>" + ParseUtils.String2Money(cg.getPrice() * cg.getChosenNum())));
			} else if (obj instanceof ShoppingCarService) {
				ShoppingCarService sc = (ShoppingCarService) obj;
				name.setText(sc.getName());
				num.setText("");
				price.setText(Html.fromHtml("<small>￥</small>" + sc.getPrice()));
			} else if (obj instanceof Gift) {
				Gift gift = (Gift) obj;
				name.setText(gift.getaName());
				if (gift.getPrice() == 0) {
					num.setText("×" + gift.getQty());
					price.setText("赠送");
				} else if (gift.getPrice() > 0) {
					num.setText("×" + gift.getQty());
					price.setText(Html
							.fromHtml("<small>￥</small>" + ParseUtils.String2Money(gift.getPrice() * gift.getQty())));
				} else {
					num.setText("");
					price.setText(Html.fromHtml(
							"-<small>￥</small>" + ParseUtils.String2Money(Math.abs(gift.getPrice() * gift.getQty()))));
				}
			} else if (obj instanceof Coupon) {
				Coupon coupon = (Coupon) obj;
				name.setText("优惠券");
				num.setText("×1");
				price.setText(Html
						.fromHtml("-<small>￥</small>" + ParseUtils.String2Money(Float.valueOf(coupon.getcValue()))));
			}
			fm_confirmorder_layout.addView(convertView);
		}
	}

	/**初始化View*/
	public void initView() {
		if (shopItem != null) {
			headTitle.setText("确认订单 - " + shopItem.getsName());
		}
		headTitle.setOnClickListener(this);
		// 添加地址
		fm_confirmorder_putaddr.setOnClickListener(this);
		// 选择地址
		fm_confirmorder_chooseaddr.setOnClickListener(this);
		// 管理地址
		fm_confirmorder_myAddr.setOnClickListener(this);
		// 选择外送时间
		fm_confirmorder_selecttime.setOnClickListener(this);
		// 添加备注
		fm_confirmorder_remarks.setOnClickListener(this);
		// 优惠券
		fm_confirmorder_coupons.setOnClickListener(this);
		// 移除优惠券
		coupons_arrow_close.setOnClickListener(this);
		// 发票
		fm_confirmorder_bill_cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if (isChecked) {
					fm_confirmorder_bill_cb.setBackgroundResource(R.raw.checkbox2);
					fm_confirmorder_bill_layout.setVisibility(View.VISIBLE);
				} else {
					fm_confirmorder_bill_cb.setBackgroundResource(R.raw.checkbox1);
					fm_confirmorder_bill_layout.setVisibility(View.GONE);
				}
			}
		});
		// 跳转到查看订单状态
		fm_confirmorder_submit.setOnClickListener(this);
		// 返回按钮
		back.setOnClickListener(this);
		// 设置时间控件选择监听
		fm_confirmorder_selecttime_view.setSelectTimeListener(new SelectTimeListener() {
			@Override
			public void selectTime(DeliverTime time) {
				if (time != null) {
					deliverTime = time;
					deliverTimeTv.setText(deliverTime.getContent());
				}
			}
		});

	}

	@Override
	public void onResume() {
		super.onResume();
		launchCoupon = false;
		EventBus.getDefault().register(this);
		userInfo = mUserModel.getUserInfo();
		mActController.openBtn();
		initView();
		initData();
		refreshData();
		if (deliverTimes == null) {
			initDeliverTime();
		}
		initShoppingData();
		// 重新生成时间戳
		currentTime = System.currentTimeMillis();
		fm_confirmorder_scroll.fullScroll(ScrollView.FOCUS_UP);
	}

	@Override
	public void onPause() {
		super.onPause();
		EventBus.getDefault().unregister(this);
	}

	@Override
	public void onClick(View v) {
		// 获取用户登录的Id
		String uid = mUserModel.getUserInfo().getuId();
		switch (v.getId()) {
		case R.id.fm_confirmorder_selecttime:
			if (deliverTimes != null) {
				LogUtil.d("deliverTimes", "" + deliverTimes.toString());
				fm_confirmorder_selecttime_view.refreshData(deliverTimes);
			} else {
				showToast("无法获取外送时间!");
			}
			break;

		case R.id.fm_confirmorder_myAddr:
			// 管理地址
			Object fmObj = ConfirmOrderActivity_.class.getName();
			Info<Object> fmInfoBean = new Info<Object>(fmObj, ConfirmOrderActivity_.class);
			openActivity4Result(SelectAddrActivity_.class, fmInfoBean, 1);
			break;

		case R.id.fm_confirmorder_chooseaddr:
			// 选择地址
			Info<Object> inf = new Info<Object>(ConfirmOrderActivity_.class.getName());
			inf.setOrigin(ConfirmOrderActivity_.class);
			openActivity4Result(SelectAddrActivity_.class, inf, 1);
			break;

		case R.id.fm_confirmorder_putaddr:
			if (mUserModel.getUserInfo() != null && !mUserModel.getUserInfo().getuId().equals("")) {
				Info<String> fmInfo = new Info<String>("ConfirmOrderFragment");
				fmInfo.setOrigin(ConfirmOrderActivity_.class);
				openActivity(PutAddrActivity_.class, fmInfo);
			} else {
				showToast("请先登录!");
				openActivity(LoginActivity_.class, null);
			}
			break;

		case R.id.fm_confirmorder_remarks:
			// 修改备注
			String remarks = remarkTx.getText().toString();
			Info<String> inf2 = new Info<String>(remarks);
			inf2.setOrigin(ConfirmOrderActivity_.class);
			openActivity4Result(RemarksActivity_.class, inf2, 1);
			break;

		case R.id.fm_confirmorder_coupons:
			// 选择优惠券
			if (uid != null && !"".equals(uid)) {
				couponPwdCheck();
			} else {
				Info<String> info = new Info<String>();
				info.setOrigin(ConfirmOrderActivity_.class);
				openActivity(LoginActivity_.class, info);
			}
			break;

		case R.id.coupons_arrow_close:
			showToast("移除优惠券");
			mShoppingCarModel.clearCouponList();
			initData();
			break;

		case R.id.fm_confirmorder_submit:
			// 确认下单
			if (mUserModel.getUserInfo() != null && !mUserModel.getUserInfo().getuId().equals("")) {
				if (addr == null) {
					dismissProgressDialog();
					showToastOnUI("请选择外送地址");
					return;
				}
				mActController.sendOrder();
			} else {
				showToast("请先登录!");
				openActivity(LoginActivity_.class, null);
			}
			break;
		case R.id.back:
		case R.id.head_title:
			if (mActController.isClose())
				return;
			handleBack();
			break;

		case R.id.pay_online:
			mActController.clickView(onlineLayout);
			break;

		case R.id.pay_deliver:
			mActController.clickView(deliverLayout);
			break;
		}
	}

	/**
	 * 重组时间数据
	 */
	public void resetData(List<DeliverTime> timeList) {
		for (int i = 0; i < timeList.size(); i++) {
			DeliverTime deliverTime = timeList.get(i);
			if (i == 0) {
				today = convertTime(System.currentTimeMillis(), "yyyy-MM-dd");
			}
			if (!today.equals(deliverTime.getDate())) {
				deliverTime.setContent("明天" + deliverTime.getContent());
			}
		}
	}

	public static String convertTime(long time, String format) {
		if (time == 0) {
			return "";
		}
		Date date = new Date(time);
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(date);
	}

	private void handleBack() {
		mShoppingCarModel.clearCouponList();
		finish();
	}

	protected void obtainInfo() {
		if (hasInfo()) {
			Object bean = mInfo.getData();
			if (bean != null) {
				if (bean instanceof MyAddress) {
					// 刷新地址
					addr = (MyAddress) bean;

				} else if (bean instanceof String) {
					// 刷新备注
					remarkContent = (String) bean;

				} else if (bean instanceof List) {
					coupon = (List<Coupon>) bean;
					mShoppingCarModel.setShoppingCarCounponList(coupon);

				} else if (bean instanceof SendOrderData) {
					SendOrderData data = (SendOrderData) bean;
					shopItem = data.getShopItem();
					shopDetail = data.getShopDetail();

				}
				refreshData();
			}
		}

	}

	// @Background
	// void queryUserInfo() {
	// if ( !NetWorkCenter.isNetworkConnected(getActivity()) ) {
	// showToast("网络根本没连,赶紧检查");
	// return;
	// }
	// try {
	// PhonePwdReq phonePwdReq = new PhonePwdReq();
	// UserInfo userInfo = userController.getUserInfo();
	// if(userInfo.getuId()!=null && !userInfo.getuId().equals("") &&
	// userInfo.getPwd()!=null && !userInfo.getPwd().equals(""))
	// {
	// if ( LocationLoopService.getInstance(mContext).getChosenAddr()!=null ) {
	// phonePwdReq.setLat(LocationLoopService.getInstance(mContext).getChosenAddr().getLat());
	// phonePwdReq.setLng(LocationLoopService.getInstance(mContext).getChosenAddr().getLng());
	// } else {
	// if ( mBaiduLocationService.getLastLocation()!=null ){
	// phonePwdReq.setLat(mBaiduLocationService.getLastLocation().getLatitude()+"");
	// phonePwdReq.setLng(mBaiduLocationService.getLastLocation().getLongitude()+"");
	// } else {
	// phonePwdReq.setLat("");
	// phonePwdReq.setLng("");
	// }
	// }
	// phonePwdReq.setuId(userInfo.getuId());
	// phonePwdReq.setPwd(userInfo.getPwd());
	// Result<UserInfo> result = userController.v2_1queryUserInfo(phonePwdReq);
	// showView(result);
	// LogUtil.i("phonePwdReq", phonePwdReq.toString());
	// }
	// else
	// {
	// fm_confirmorder_chooseaddr.setVisibility(View.GONE);
	// fm_confirmorder_myAddr.setVisibility(View.GONE);
	// fm_confirmorder_putaddr.setVisibility(View.VISIBLE);
	// }
	// } catch (RetrofitError e) {
	// switch (e.getKind()) {
	// case HTTP:
	// showToast("很遗憾，网络不给力！");
	// return;
	// case CONVERSION:
	// showToast("很抱歉，服务器又任性了！");
	// return;
	// case NETWORK:
	// showToast("很遗憾，网络不给力！");
	// return;
	// case UNEXPECTED:
	// showToast("很抱歉，服务器又任性了！");
	// return;
	// }
	// }
	// }

	// @UiThread
	// void showView(Result<UserInfo> result) {
	// if(result!=null && result.code==1)
	// {
	// showUI();
	// }
	// else
	// {
	// fm_confirmorder_chooseaddr.setVisibility(View.VISIBLE);
	// fm_confirmorder_myAddr.setVisibility(View.GONE);
	// fm_confirmorder_putaddr.setVisibility(View.GONE);
	// }
	// }

	private PwdCheckReq pwdCheckReq = new PwdCheckReq();
	private Dialog dialog = new Dialog();

	private boolean launchCoupon = false;
	/**
	 * 优惠券验证密码
	 */
	@Background
	void couponPwdCheck() {
		if (!NetWorkCenter.isNetworkConnected(mActivity)) {
			showToastOnUI("网络根本没连,赶紧检查");
			return;
		}
		userInfo = mUserModel.getUserInfo();
		pwdCheckReq.setuId(userInfo.getuId());
		pwdCheckReq.setPwd(userInfo.getPwd());
		try {
			CodeResult result = mUserModel.pwdCheck(pwdCheckReq);
			if (result != null && result.getCode() == -1) {
				runOnUI(new Runnable() {
					@Override
					public void run() {
						dialog.LoginToast(mContext, "您的账号已在其他地方登录，请重新登录", UseCouPonActivity_.class);
					}
				});
			} else if (result != null && result.getCode() == 1) {
				Info<ShopItem> info = new Info<ShopItem>(shopItem);
				info.setOrigin(ConfirmOrderActivity_.class);
				if( !launchCoupon ){
					openActivity4Result(UseCouPonActivity_.class, info, 1);
					launchCoupon = true;
				}
				
			} else if (result != null && result.getCode() == -2) {
				showToastOnUI("很抱歉,服务器又任性了");
			}
		} catch (RetrofitError e) {
			switch (e.getKind()) {
			case HTTP:
				showToastOnUI("很遗憾,网络不给力");
				return;
			case CONVERSION:
				showToastOnUI("很抱歉,服务器又任性了");
				return;
			case NETWORK:
				showToastOnUI("很遗憾,网络不给力");
				return;
			case UNEXPECTED:
				showToastOnUI("很抱歉,服务器又任性了");
				return;
			}
		}
	}
	

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		obtainInfo();
	}
	
	@Override
	public boolean backOperate() {
		handleBack();
		return true;
	}

}
